package com.hmall.item.web;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hmall.common.Client.OrderClient;
import com.hmall.common.dto.PageDTO;
import com.hmall.item.mqconstants.MqConstants;
import com.hmall.item.pojo.Item;
import com.hmall.item.service.IItemService;
import io.swagger.annotations.Api;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
@RestController
@RequestMapping("item")
public class ItemController {
    @Autowired
    private OrderClient orderClient;

    @Autowired
    private IItemService itemService;
    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
     * 分页查询商品列表
     *
     * @param page
     * @param size
     * @return
     */
    @GetMapping("list")
    public PageDTO<Item> QueryPageByItem(@RequestParam("page") Integer page,
                                         @RequestParam("size") Integer size) {
        //1-利用mybatis-puls进行分页查询
        Page<Item> itemPage = itemService.page(new Page<>(page, size));
        //2-封装结果并返回
        return new PageDTO<>(itemPage.getTotal(), itemPage.getRecords());

    }

    /**
     * 根据商品id查看商品详情
     */
    @GetMapping("/{id}")
    public Item queryItemById(@PathVariable("id") Long id) {
        return itemService.getById(id);
    }

    /**
     * 新增商品
     */
    @PostMapping
    public void saveItem(@RequestBody Item item) {
        item.setCreateTime(new Date());
        item.setUpdateTime(new Date());
        item.setStatus(2);
        itemService.save(item);
        /**
         * 新增商品发送消息
         */
        rabbitTemplate.convertAndSend(MqConstants.ITEM_EXCHANGE, MqConstants.ITEM_INSERT_ROUTING_KEY, item.getId());
    }

    /**
     * 根据id修改商品状态
     */
    @Transactional
    @PutMapping("status/{id}/{status}")
    public void updateItemStatus(@PathVariable("id") Long id, @PathVariable("status") Integer status) {
        itemService.update().set("status", status).eq("id", id).update();

        rabbitTemplate.convertAndSend(MqConstants.ITEM_EXCHANGE, MqConstants.ITEM_INSERT_ROUTING_KEY, id);

    }

    /**
     * 根据id删除商品
     */
    @Transactional
    @DeleteMapping("{id}")
    public void deleteItemById(@PathVariable("id") Long id) {
        itemService.removeById(id);

        rabbitTemplate.convertAndSend(MqConstants.ITEM_EXCHANGE, MqConstants.ITEM_DELETE_ROUTING_KEY, id);
    }

    /**
     * 修改商品信息
     */
    @PutMapping
    public void updateItem(@RequestBody Item item) {
        item.setUpdateTime(new Date());
        itemService.updateById(item);

        rabbitTemplate.convertAndSend(MqConstants.ITEM_EXCHANGE, MqConstants.ITEM_INSERT_ROUTING_KEY, item.getId());
    }

    /**
     * 根据订单状态和商品id扣减库存
     */
    @PutMapping("/stock/{itemId}/{num}")
    public void decreaseStock(@PathVariable("itemId") Long itemId, @PathVariable("num") Integer num) {

        itemService.decreaseStock(itemId, num);

    }


    /**
     * 根据订单状态和商品id增加库存
     */
    @PutMapping("/stock/add/{itemId}/{num}")
    public void addStock(@PathVariable("itemId") Long itemId, @PathVariable("num") Integer num) {
        itemService.addStock(itemId, num);

    }
}


